package com.defangit.applicant.orm;

import com.defangit.applicant.domain.Ticket;
import com.defangit.applicant.domain.TicketType;
import laogen.hibernate.CommonFinder;
import laogen.hibernate.CommonService;
import laogen.lang.exception.ParameterNotValidException;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * Created by Administrator on 2019/3/11.
 */
public class TicketService extends CommonService<Ticket> {
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(Ticket.class);
    //添加报名活动票据
    public Ticket addTicket(int ticketTypeid, Ticket ticket){
        TicketType ticketType = getSession().get(TicketType.class,ticketTypeid);
        if (ticketType==null)throw ParameterNotValidException.emptyOrNull("票种id");
        if (ticket==null) throw ParameterNotValidException.zeroIdOrEntityNonExist("要添加的票");
        if (ticketType.getAllowedTickets()<=0) ParameterNotValidException.zeroIdOrEntityNonExist("票种可用数量");
        add(ticket);
        commitSession();
        return ticket;
    }
    //报名服务api
//    public List<Ticket> applicant(Integer ticketTypeId,List<String> openId){
//        if (ticketTypeId==null||ticketTypeId==0) throw ParameterNotValidException.zeroIdOrEntityNonExist("票种id");
//        //获取当前票种实体
//        TicketType ticketType = getSession().get(TicketType.class,ticketTypeId);
//        //验证此实体是否可用(1.是否存在 2.剩余数量是否大于添加数量)
//        if (ticketType==null)throw ParameterNotValidException.zeroIdOrEntityNonExist("当前票种");
//        if(ticketType.getAllowedTickets()<openId.size()) throw ParameterNotValidException.constraintNotSatisfy("当前票种最大可用值","提交票数");
//        //根据传入数据生成ticket表
//        List<Ticket> list = new ArrayList<>();
//        for (String str:openId){
//            Ticket ticket = new Ticket();
//            ticket.setTicketName(ticketType.getTicketName())
//                    .setTicketType(ticketType)
//                    .setOpenids(str);
//            list.add(ticket);
////            ticketType.addTicket(ticket);
//        }
//        //将修改信息提交到数据库中
//        saveOrUpdate(list);
//        commitSession();
//        return list;  //返回ticket信息，用于页面显示
//    }
//    //要删除/关闭的票种
//    public boolean delTicket(/*int enrollId,*/Integer id){
////        Enroll enroll = getSession().get(Enroll.class,enrollId);
////        if (enroll==null) throw ParameterNotValidException.zeroIdOrEntityNonExist("报名活动id");
//        if (id==null||id==0) throw ParameterNotValidException.zeroIdOrEntityNonExist("票种id");
//        //获取当前票种实体
//        Ticket ticket = getSession().get(Ticket.class,id);
//        //获取当前对象(ticket)父类实体(enroll)并取消父子级联
//        ticket.getTicketType().getTickets().remove(ticket);
//        //当前对象父类关联置空
//        ticket.setTicketType(null);
////        Ticket ticket = get(id).orElseThrow(()->ParameterNotValidException.zeroIdOrEntityNonExist("要删除的票种"));
//        delete(ticket);
//        commitSession();
//        return true;
//    }
    //Ticket的查询构造

    public TicketFinder findAll(){
        return new TicketFinder();
    }

    private class TicketFinder extends CommonFinder<Ticket> {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Ticket.class);

        @Override //为Ticket实体类提供criteria查询方法
        public Criteria getCriteria() {
            return detachedCriteria.getExecutableCriteria(getSession());
        }

        @Override   //为Ticket实体类提供detachedcriteria查询方法
        public DetachedCriteria getDetachedCriteria() {
            return detachedCriteria;
        }
        public TicketFinder ticket(Integer ticketid){//创建联合查询,Ticket实体类服务提供ticket实体属性Id查询结果
            detachedCriteria = detachedCriteria.createAlias("tickets","ticket").add(Restrictions.eq("ticket.id",ticketid));
            return this;
        }
    }


    
}
